<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../config.inc.php");

$_LangPath = "joinnumbercheck";	//語言檔參數，務必放在最前面

$intLID = (int)$_data["LottoID"];
$intBT = (int)$_data["Bet_Type"];
$intNumber = (int)$_data["Number"];
if($intNumber != 1)$intNumber = 0;
//找出 Apoint
$intAN = (int)$_arrGLineNumToApointNum[$intLID][$intBT][$intNumber];

include_once(__Language_Path."/selectlanguage_member.php");
include_once(__Member_Path."/logincheck_nodb.php");
include_once(__Common_Path."/ExchangeRate.inc.php");
include_once(__Modules_Path."/joinnumber.lib.php");
include_once(__Common_Path."/limit/LottoLimit_". $intLID. ".inc.php");
include_once(__Common_Path."/betparam/BetParam_". $intLID. ".inc.php");
include_once(__Common_Path."/todaygame/Gamble".$intLID.".inc.php");
include_once(__Root_Path."/tmp/credit-". $_SESSION["Member_Info"]->ID .".php");
if($_SESSION["Member_Info"] -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}

include(__Common_Path.'/todaygame/TodayGame'. $intLID .'_'. $intBT .'.inc.php');

//基本值
$ChoiceNumberList = null;
$ChoiceCounter = 0;
$finalArray = array();
$lineFeedCount = 5;		//下單內容，幾個號碼一行
$GroupCounter = 2;		//預設2個號碼為一組
$OtherMixBetLimit = '';
$OtherMaxEventBetLimit = '';
$SpecialLimit = '';
$StakeNumber = '';


//找出公司最小單注
$flMemERate	= (empty($_ECRate[$_SESSION[Member_Info]->ERID][Rate])) ? 1 : $_ECRate[$_SESSION[Member_Info]->ERID][Rate];
$CompanyMixBet	= floor($_arrBetParam[$_data["Bet_Type"]][$intAN]["Com_BetLimit"] / $flMemERate);

//最低限額轉換匯率
$_arrBetParam[$intBT][$intAN][MinStake] = (empty($_arrBetParam[$intBT][$intAN][MinStake])) ? 0 : floor($_arrBetParam[$intBT][$intAN][MinStake] / $flMemERate);

//比對取出單注最低
$_arrLottoLimit[$intBT][0]	= (empty($_arrLottoLimit[$intBT][0])) ? 0 : floor($_arrLottoLimit[$intBT][0] / $flMemERate);
$_arrLottoLimit[$intBT][1]	= (empty($_arrLottoLimit[$intBT][1])) ? 0 : floor($_arrLottoLimit[$intBT][1] / $flMemERate);

$JoinMixBet = min(((int)$_SESSION[Member_Info] -> arrBetLimit[$intLID][$intBT]), $_arrLottoLimit[$intBT][1], $CompanyMixBet);

//剩餘信用額度
$_arrMemStake["_SUM"]	= ($_arrMemStake["_SUM"]) ? $_arrMemStake["_SUM"] : 0;
$CreditSurplus 		= $_SESSION["Member_Info"]->CreditLine - $_arrMemStake["_SUM"];

//比對取出單場最高(取最小)
$JoinMaxEventBet = min(((int)$_SESSION[Member_Info] -> arrEventLimit[$intLID][$intBT]) , $_arrLottoLimit[$intBT][0]);

//特殊單注單場限額(顯示用)
if ($_SESSION[Member_Info]->arrBetLimit[$intLID][$intBT] != $JoinMixBet) {
	//另外單注限額
	$OtherMixBetLimit = '<font class="left_jn_pg_limit">'. $JoinMixBet .'</font>';
}

if ($_SESSION[Member_Info]->arrEventLimit[$intLID][$intBT] != $JoinMaxEventBet) {
	//另外單場限額
	$OtherMaxEventBetLimit = '<font class="left_jn_pg_limit">'. $JoinMaxEventBet .'</font>';
}

//賠率表
list($intGID, $arrGData) = each($_arrGamble);
switch ($intBT) {
	case 7:
		//三全中
		$JoinOddsTitle = $_strJoinNumberCheck[AllOfThreeTitle] . $_strJoinNumberCheck[AColon];
		$JoinOddsValue = $_strJoinNumberCheck[AllOfThreeTitle] .'&nbsp;<font class="left_jn_pg_odds">'. $_arrTodayGame[$intGID][7][0][Odds] .'</font>';
		$OrderOddsID = $_arrTodayGame[$intGID][7][0][OddsID];
		$OrderOdds = $_arrTodayGame[$intGID][7][0][Odds];
		$OrderGambleID = $_arrTodayGame[$intGID][7][0][GambleID];
		$GroupCounter = 3;	// 3個號碼為一組
		$MaxOdds = $OrderOdds;
		break;

	case 8:
		//三中二
		$JoinOddsTitle = $_strJoinNumberCheck[TwoOfThreeTitle] . $_strJoinNumberCheck[AColon];
		$JoinOddsValue = $_strJoinNumberCheck[WinTwoTitle] .'&nbsp;<font class="left_jn_pg_odds">'. $_arrTodayGame[$intGID][8][0][Odds] .'</font><br>'. $_strJoinNumberCheck[WinThreeTitle] .'&nbsp;<font class="left_jn_pg_odds">'. $_arrTodayGame[$intGID][8][1][Odds] .'</font>';
		$OrderOddsID = $_arrTodayGame[$intGID][8][0][OddsID] .'|'. $_arrTodayGame[$intGID][8][1][OddsID];
		$OrderOdds = $_arrTodayGame[$intGID][8][0][Odds] .'|'. $_arrTodayGame[$intGID][8][1][Odds];
		$OrderGambleID = $_arrTodayGame[$intGID][8][0][GambleID];
		$GroupCounter = 3;	// 3個號碼為一組
		$MaxOdds = max($_arrTodayGame[$intGID][8][0][Odds], $_arrTodayGame[$intGID][8][1][Odds]);
		break;

	case 9:
		//二全中
		$JoinOddsTitle	= $_strJoinNumberCheck[AllOfTwoTitle] . $_strJoinNumberCheck[AColon];
		$JoinOddsValue	= $_strJoinNumberCheck[AllOfTwoTitle] .'&nbsp;<font class="left_jn_pg_odds">'. $_arrTodayGame[$intGID][9][0][Odds] .'</font>';
		$OrderOddsID 	= $_arrTodayGame[$intGID][9][0][OddsID];
		$OrderOdds 	= $_arrTodayGame[$intGID][9][0][Odds];
		$OrderGambleID	= $_arrTodayGame[$intGID][9][0][GambleID];
		$MaxOdds 	= $OrderOdds;
		break;

	case 10:
		//二中特
		$JoinOddsTitle	= $_strJoinNumberCheck[SpecialOfTwoTitle] . $_strJoinNumberCheck[AColon];
		$JoinOddsValue	= $_strJoinNumberCheck[WinSpecialTitle] .'&nbsp;<font class="left_jn_pg_odds">'. $_arrTodayGame[$intGID][10][0][Odds] .'</font><br>'. $_strJoinNumberCheck[WinTwoTitle] .'&nbsp;<font class="left_jn_pg_odds">'. $_arrTodayGame[$intGID][10][1][Odds] .'</font>';
		$OrderOddsID 	= $_arrTodayGame[$intGID][10][0][OddsID] .'|'. $_arrTodayGame[$intGID][10][1][OddsID];
		$OrderOdds 	= $_arrTodayGame[$intGID][10][0][Odds] .'|'. $_arrTodayGame[$intGID][10][1][Odds];
		$OrderGambleID	= $_arrTodayGame[$intGID][10][0][GambleID];
		$MaxOdds 	= max($_arrTodayGame[$intGID][10][0][Odds], $_arrTodayGame[$intGID][10][1][Odds]);
		break;

	case 11:
		//特串
		$JoinOddsTitle = $_strJoinNumberCheck[SpecialLinkTitle] . $_strJoinNumberCheck[AColon];
		$JoinOddsValue = $_strJoinNumberCheck[SpecialLinkTitle] .'&nbsp;<font class="left_jn_pg_odds">'. $_arrTodayGame[$intGID][11][0][Odds] .'</font>';
		$OrderOddsID = $_arrTodayGame[$intGID][11][0][OddsID];
		$OrderOdds = $_arrTodayGame[$intGID][11][0][Odds];
		$OrderGambleID = $_arrTodayGame[$intGID][11][0][GambleID];
		$MaxOdds = $OrderOdds;
		break;

	default:
		$Err_Message 		= $_strJoinNumberCheck[Err_Message];
		$Err_HiddenMessage 	= $_strJoinNumberCheck[Err_Message];
		include("wager_error.php");
		break;
}

//包車
if($intNumber == 0){
	//排序+做列表
	if (!is_array($_data["ChoiceNumber"])) {
		$Err_Message 		= $_strJoinNumberCheck[Err_Message];
		$Err_HiddenMessage 	= $_strJoinNumberCheck[Err_Message];
		include("wager_error.php");
	}

	sort($_data[ChoiceNumber]);
	foreach ($_data[ChoiceNumber] as $value) {
		$ChoiceCounter++;
		$value = sprintf("%02s", $value);

		if ($ChoiceCounter >= $lineFeedCount && $ChoiceCounter % $lineFeedCount == 0) {
			$ChoiceNumberList .= '<font class="left_jn_pg_choice_no">'. $value . '</font><br>';
		} else {
			$ChoiceNumberList .= '<font class="left_jn_pg_choice_no">'. $value .'</font>, ';
		}
		
		$OrderStakeNumber .= $value . '|';
	}
	//下注號碼
	$OrderStakeNumber = substr($OrderStakeNumber, 0, -1);

	if (substr($ChoiceNumberList, -1) == '>') {
		$ChoiceNumberList = substr($ChoiceNumberList, 0, -4);
	} else {
		$ChoiceNumberList = substr($ChoiceNumberList, 0, -2);
	}
	//取得下注總組數
	$ChoiceGroup = count(combination($_data[ChoiceNumber], $GroupCounter));

//排碰
}else{
	$arrCTNo = array();
	$arrChoiceTNo = array();
	$arrChoiceList = array();
	$arrOrderList = array();
	$arrChoiceCounter = array();
	for($i = 0; $i < $GroupCounter; $i++){
		$arrTmp = explode(",", $_data["ChoiceTNo".($i + 1)]);
		foreach($arrTmp as $strN){
			$intN = (int)$strN;
			$arrCTNo[$intN] = $i;
		}
	}
	foreach($arrCTNo as $intN => $intCNo){
		$arrChoiceTNo[$intCNo][] = sprintf("%02s", $intN);
	}
	for($i = 0; $i < $GroupCounter; $i++){
		if(!is_array($arrChoiceTNo[$i])){
			$Err_Message 		= $_strJoinNumberCheck[Err_Message];
			$Err_HiddenMessage 	= $_strJoinNumberCheck[Err_Message];
			include("wager_error.php");

		}elseif(sizeof($arrChoiceTNo[$i]) > 12){
			$Err_Message 		= $_strJoinNumberCheck[Err_Message];
			$Err_HiddenMessage 	= $_strJoinNumberCheck[Err_Message];
			include("wager_error.php");
		}
		$arrChoiceList[$i] = '[<font class="left_jn_pg_choice_no">'.join(",", $arrChoiceTNo[$i]). '</font>]';
		$arrOrderList[$i] = join(",", $arrChoiceTNo[$i]);
		$arrChoiceCounter[$i] = sizeof($arrChoiceTNo[$i]);
	}
	$ChoiceNumberList = implode(" x <br>", $arrChoiceList);
	$OrderStakeNumber = implode("|", $arrOrderList);
	$ChoiceCounter = implode(" x ", $arrChoiceCounter);
	//取得下注總組數
	$ChoiceGroup = count(combinationForLine($arrChoiceTNo, $GroupCounter));
}

//特殊限額顯示
if ($OtherMixBetLimit || $OtherMaxEventBetLimit) {
	$SpecialLimit = '  <tr class="left_jn_pg_sp_limit_title"><td colspan="2" class="left_jn_pg_sp_limit_title">'. $_strJoinNumberCheck[SpecialLimitTitle] .'</td></tr>';
	if ($OtherMixBetLimit) {
		$SpecialLimit .= '<tr class="left_jn_pg_sp_limit_con"><td class="left_jn_pg_sp_limit_tip">'. $_strJoinNumberCheck[MinWagerStakeTitle] .'</td><td class="left_jn_pg_sp_limit_value">'. $OtherMixBetLimit .'</td></tr>';
	}
	if ($OtherMaxEventBetLimit) {
		$SpecialLimit .= '<tr class="left_jn_pg_sp_limit_con"><td class="left_jn_pg_sp_limit_tip">'. $_strJoinNumberCheck[MaxRoundLimitTitle] .'</td><td class="left_jn_pg_sp_limit_value">'. $OtherMaxEventBetLimit .'</td></tr>';
	}
}

//取得最高派彩及未超過派彩的最高下注金額
$MaxPayOff = $_arrBetParam[$intBT][$intAN][MaxPayOff];
$MaxStakePayOff = floor($MaxPayOff / $MaxOdds);


//取得 group money ... 剩餘信用額度 / 組數 ....再跟(會員,公司,系統,派彩的最高下注金額)比取最低
$ChoiceGroupMoney = floor($CreditSurplus / $ChoiceGroup);
$ChoiceGroupMoney = min($JoinMixBet , $ChoiceGroupMoney , $MaxStakePayOff);
$MinChoiceGroupMoney = $_arrBetParam[$intBT][$intAN][MinStake];

//產生 CheckKeyValue
$arrCheckKey = split(" ",microtime());
$CheckKeyValue1 = $arrCheckKey[0];
$CheckKeyValue2 = $arrCheckKey[1];

$fp = @file(__Membertemplate_Path."/joinnumber_checkout.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);

while(list($k, $v) = each($_strJoinNumberCheck)){
	$fp = str_replace("{".$k."}", $v, $fp);
}
$fp = str_replace("{ChoiceGroupMoney}", $ChoiceGroupMoney, $fp);
$fp = str_replace("{ChoiceGroup}", $ChoiceGroup, $fp);
$fp = str_replace("{ChoiceCounter}", $ChoiceCounter, $fp);
$fp = str_replace("{MinChoiceGroupMoney}", $MinChoiceGroupMoney, $fp);

$fp = str_replace("{Account}", $_SESSION[Member_Info] -> Account, $fp);
$fp = str_replace("{Currency}", $_ECRate[$_SESSION[Member_Info] -> ERID][$_intLang], $fp);
$fp = str_replace("{CreditLine}", $_SESSION[Member_Info] -> CreditLine, $fp);
$fp = str_replace("{CreditAlreadyUse}", $_arrMemStake["_SUM"], $fp);
$fp = str_replace("{CreditSurplus}", $CreditSurplus, $fp);
$fp = str_replace("{MaxStakeMoney}", $ChoiceGroupMoney, $fp);
$fp = str_replace("{MinStakeMoney}", $_arrBetParam[$intBT][$intAN][MinStake], $fp);
$fp = str_replace("{MaxStakePayOff}", $MaxStakePayOff, $fp);
$fp = str_replace("{JoinMixBet}", $JoinMixBet, $fp);

$fp = str_replace("{MinWagerStake}", $_SESSION[Member_Info] -> arrBetLimit[$intLID][$intBT], $fp);
$fp = str_replace("{MaxRoundLimit}", $_SESSION[Member_Info] -> arrEventLimit[$intLID][$intBT], $fp);
$fp = str_replace("{SpecialLimit}", $SpecialLimit, $fp);
$fp = str_replace("{JoinOddsTitle}", $JoinOddsTitle, $fp);
$fp = str_replace("{JoinOddsValue}", $JoinOddsValue, $fp);
$fp = str_replace("{ChoiceNumberList}", $ChoiceNumberList, $fp);
$fp = str_replace("{Periods}", $arrGData[SerialNo], $fp);
$fp = str_replace("{MaxPayOff}", $MaxPayOff, $fp);

$fp = str_replace("{OrderGambleID}", $OrderGambleID, $fp);
$fp = str_replace("{Bet_Type}", $intBT, $fp);
$fp = str_replace("{Number}", $intNumber, $fp);
$fp = str_replace("{OrderOddsID}", $OrderOddsID, $fp);
$fp = str_replace("{OrderOdds}", $OrderOdds, $fp);
$fp = str_replace("{OrderStakeNumber}", $OrderStakeNumber, $fp);
$fp = str_replace("{LottoID}", $intLID, $fp);
$fp = str_replace("{UserID}", $_SESSION[Member_Info] -> ID, $fp);
$fp = str_replace("{CheckKeyValue1}", $CheckKeyValue1, $fp);
$fp = str_replace("{CheckKeyValue2}", $CheckKeyValue2, $fp);
$fp = str_replace("{sid}", $_data[sid], $fp);

//輸出
WebOutputHTML($fp, 1);
?>